<?php
namespace app\index\controller;

use app\index\model\User;
use think\Request;
use app\index\model\Thread;
use app\index\model\Like;
use app\index\model\Album;
class Near extends Base
{
    public function index()
    {
		$arrname = Album::all(["tid"=>49]);
		print_r($arrname->toArray() );
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V5.1<br/><span style="font-size:30px">12载初心不改（2006-2018） - 你值得信赖的PHP框架</span></p></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="eab4b9f840753f8e7"></think>';
    }

    public function hello($name = 'ThinkPHP5')
    {

        User::update(['point'=>time()],['id'=>1]);
        dump(User::get(1));

    }
	//点赞
    public function like()
    {
		$tid = input('tid');
		$uid = input('uid');
	    $arr = Like::get(["tid"=>$tid,"uid"=>$uid,"pid"=>0]);
		if($arr){
			 $res = $arr->delete();
			 if($res){
				return $this->_toError(['msg' => '取消成功']);
				Thread::get($tid)->setDec('goodnums');
			}else{
				return $this->_toError(['msg' => '取消失败']);
			}
		}else{
			$like           = new Like;
			$like->tid     = $tid;
			$like->uid    = $uid;
			$like->atime    = time();
			$res = $like->save();
			if($res){
				Thread::get($tid)->setInc('goodnums');
				return $this->_toError(['msg' => '点赞成功']);
			}else{
				return $this->_toError(['msg' => '点赞失败']);
			}
			
		}
	}
	//获取所有帖子
    public function loadThread()
    {
        $typeid  =  input('typeid');
		$uid  =  input('uid');
		$hot  =  input('hot');
		$page  =  input('page');
		if($typeid==0){
			if($hot==0){
				Thread::where('status','>',0)->count();
			}else{
				Thread::where('status','>',0)->count();
			}
			
		}else{
			Thread::where('status','>',0)->count();
		}
		
		
	}	
	//获取所有帖子
    public function allThread()
    {
        $typeid  =  input('typeid');
		$city  =  input('city');
		$uid  =  input('uid');
		$hot  =  input('hot');
		$page  =  input('page');
		$url = input("url");
		if($page>0){
			$limit = $page*3;
		}else{
			$limit = 3;
		}
		if($typeid == 0){
			// 查询所有
			$total = Thread::count()+3;
			if($total<$limit){
				return $this->_toError(['msg' => '没有更多了！']);
			}
			if($hot==0){
				$list = Thread::all(function($query) use($limit,$city){
					$query->where('region', $city)->limit($limit)->order('tid', 'desc');
				});				
			}else{
				$list = Thread::all(function($query) use($limit,$city){
					$query->where('region', $city)->limit($limit)->order('goodnums', 'desc');
				});				
			}

			$lists = [];
			if($list){
				 foreach($list as $arr){
					$arrname = User::get($arr['authorid']);
					$arr['url'] = $arrname['avatar'];
					$arr['author'] = $arrname['nickname'];
					//查看是否点赞当前用户
					$arrlike = Like::get(["tid"=>$arr['tid'],"uid"=>$uid,"pid"=>0]);
					$arr['like'] = $arrlike ? 1 : 0;
				    //查询帖子是否有图片
					$arrname = Album::all(["tid"=>$arr['tid']]);
                    
					if($arrname){
						$arrname = $arrname->toArray();
						$images = [];
						foreach($arrname as $arrn){
							$images[] = $url."/album/".$arrn['dir']."/".$arrn['filename'];
						}
	 
					//print_r($images);exit;
						$arr['imgList'] = $images;
						$arr['img_num'] = count($images);
					}else{
						$arr['img_num'] = 0;
						$arr['imgList'] = "";
					}
					$lastime = strtotime($arr['create_time']);
					$arr['lastime'] = $this->lastime($lastime);
					$lists[] = $arr;
				 }
				 return $this->_toSuccess(['list' => $lists ,'msg' => '获取成功']);
			}else{
				 return $this->_toError(['msg' => '获取失败']);
			}			
		}elseif($typeid > 0){
			// 指定分类
			$total = Thread::where('typeid',$typeid)->count()+3;
			if($total<$limit){
				return $this->_toError(['msg' => '没有更多了！']);
			}
			
			if($hot==0){
				$list = Thread::all(function($query) use($typeid,$city){
				$query->where('typeid', $typeid)->where('region', $city)->limit(3)->order('tid', 'desc');
			});			
			}else{
                $list = Thread::all(function($query) use($typeid,$city){
				$query->where('typeid', $typeid)->where('region', $city)->limit(3)->order('goodnums', 'desc');
			    });				
			}
			$lists = [];
			if($list){
				 foreach($list as $arr){
					$arrname = User::get($arr['authorid']);
					$arr['url'] = $arrname['avatar'];
					$arr['author'] = $arrname['nickname'];
					$arrlike = Like::get(["tid"=>$arr['tid'],"uid"=>$uid,"pid"=>0]);
					$arr['like'] = $arrlike ? 1 : 0;
					//查询帖子是否有图片
					$arrname = Album::all(["tid"=>$arr['tid']]);
                    
					if($arrname){
						$arrname = $arrname->toArray();
						$images = [];
						foreach($arrname as $arrn){
							$images[] = "/album/".$arrn['dir']."/".$arrn['filename'];
						}
	 
					//print_r($images);exit;
						$arr['imgList'] = $images;
						$arr['img_num'] = count($images);
					}else{
						$arr['img_num'] = 0;
						$arr['imgList'] = "";
					}
					$lastime = strtotime($arr['create_time']);
					$arr['lastime'] = $this->lastime($lastime);
					$lists[] = $arr;
				 }
				 return $this->_toSuccess(['list' => $lists ,'msg' => '获取成功']);
			}else{
				 return $this->_toError(['msg' => '获取失败']);
			}				
		}
 

    }	
}
